package com.utils

import android.app.Dialog
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.jph.takephoto.R
import com.jph.takephoto.model.TImage
import com.jph.takephoto.model.TResult
import com.jph.takephoto.uitl.TConstant
import com.video.CameraActivity

/**
 * 底部弹框选择照相机/相册弹框
 * Created by yubaokang on 2016/10/20.
 */
class PhotoBottomSheet private constructor() : TakePhotoCallBack() {
    val isForRN = true //提供给RN使用

    var type = 0 //0：拍照，1：相册选择，2：拍视频
    override fun setupDialog(dialog: Dialog, style: Int) {
        val view = LayoutInflater.from(activity)
            .inflate(R.layout.bottom_sheet_photo, null, false)
        view.findViewById<View>(R.id.tv_tack_photo).setOnClickListener {
            type = 0
            openCamera()
        }
        val tv_tack_video =
            view.findViewById<TextView>(R.id.tv_tack_video)
        val line_video =
            view.findViewById<View>(R.id.line_video)
        if (videoEnable) {
            tv_tack_video.visibility = View.VISIBLE
            line_video.visibility = View.VISIBLE
            tv_tack_video.setOnClickListener {
                type = 2
                startActivityForResult(
                    Intent(activity, CameraActivity::class.java),
                    TConstant.TAKE_VIDEO_MP4
                )
            }
        } else {
            tv_tack_video.visibility = View.GONE
            line_video.visibility = View.GONE
        }
        view.findViewById<View>(R.id.tv_from_gallery).setOnClickListener {
            type = 1
            openDcimMuti()
        }
        dialog.setContentView(view)
    }

    //TODO 临时处理方案
    // requestCode会被修改，但不知道哪里修改
    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        if (type == 0) {
            super.onActivityResult(TConstant.RC_PICK_PICTURE_FROM_CAPTURE, resultCode, data)
        } else if (type == 1) {
            super.onActivityResult(TConstant.RC_PICK_MULTIPLE, resultCode, data)
        } else if (type == 2) {
            //这里处理拍摄视频的回调
            val videoFilePath = data?.getStringExtra("videoFilePath")
            if (videoFilePath.isNullOrEmpty()) {
                result.onHandlerFailure("取消选择")
            } else {
                result.onHandlerSuccess(TResult.of(TImage.of(videoFilePath, TImage.FromType.OTHER)))
            }
            dialog?.dismiss()
        }
    }

    companion object {
        fun get(): PhotoBottomSheet {
            return PhotoBottomSheet()
        }
    }
}